package com.test.deencredit.view_deencredit.process_widget_deencredit;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseProcessWidgetManager_deencredit;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_Data_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.AccountList_Fr_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.BankAccount_Fr_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.OthersInfo_deencredit;
import com.test.deencredit.dialog_deencredit.BindAccountChannelDialog_Fr_deencredit;
import com.test.deencredit.dialog_deencredit.BindAccountDialog_Fr_deencredit;
import com.test.deencredit.dialog_deencredit.DeleteAccountDialog_Fr_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class BindAccountViewManager_Fr_deencredit extends BaseProcessWidgetManager_deencredit {

    private final FragmentActivity activity_deencredit;

    private LinearLayout groupLayout_deencredit;
    private TextView tipsTitleTv_deencredit, tipsDescTv_deencredit;

    private ArrayList<BankAccount_Fr_deencredit> accounts_deencredit = new ArrayList<>();

    private final View.OnClickListener addAccountCl_deencredit;

    private String bindChannelMobile_deencredit = "";

    public BindAccountViewManager_Fr_deencredit(View view, ProcessInfo_Data_deencredit data, String pageTag){
        super(view,data,pageTag);
        activity_deencredit = (FragmentActivity) view.getContext();
        addAccountCl_deencredit = v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"ADD_CLICK","");
            new BindAccountDialog_Fr_deencredit(activity_deencredit, page_deencredit,
                    mobile -> {
                        bindChannelMobile_deencredit = mobile;
                        HttpUtil_deencredit.getInstance_deencredit().requestAccountList_Fr_deencredit(activity_deencredit,this::refreshAccountList_deencredit);
                    }
            ).show();
        };
     }

    @Override
    protected void init_deencredit(){
        groupLayout_deencredit = mView_deencredit.findViewById(R.id.vpbf_layout_group);
        tipsTitleTv_deencredit = mView_deencredit.findViewById(R.id.vpbf_tv_tips_title);
        tipsDescTv_deencredit = mView_deencredit.findViewById(R.id.vpbf_tv_tips_desc);

        mView_deencredit.findViewById(R.id.vpbf_view_add).setOnClickListener(v -> {
            addAccountCl_deencredit.onClick(v);
        });

        String tipsTitle = AppShowCustomize_deencredit.getInstance_deencredit().getBindAccountTipsTitle_deencredit();
        String tipsDesc = AppShowCustomize_deencredit.getInstance_deencredit().getBindAccountTipsDesc_deencredit();

        if (!TextUtils.isEmpty(tipsTitle) && !TextUtils.isEmpty(tipsDesc)){
            tipsTitleTv_deencredit.setText(tipsTitle);
            tipsDescTv_deencredit.setText(tipsDesc);
        }

    }

    @Override
    public boolean checkInput_deencredit() {
        return !accounts_deencredit.isEmpty();
    }

    @Override
    public String getInput_deencredit() {
        return String.valueOf(accounts_deencredit.size());
    }

    //更新列表
    public void refreshAccountList_deencredit(){
        groupLayout_deencredit.removeAllViews();

        accounts_deencredit = AccountList_Fr_deencredit.getInstance_deencredit().getBankAccountList_deencredit();

        int num = 1;
        int i= accounts_deencredit.size()-1;
        while (i> accounts_deencredit.size()-3 || i>=0){

            if (i>=0) addAccount_deencredit(accounts_deencredit.get(i),num++);
            else {
                View view = View.inflate(mView_deencredit.getContext(), R.layout.item_add_account_fr_deencredit, null);
                TextView titleView = view.findViewById(R.id.iaaf_tv_title);
                titleView.append(" "+(num++));
                view.findViewById(R.id.iaaf_view_click).setOnClickListener(addAccountCl_deencredit);
                groupLayout_deencredit.addView(view);
            }

            i--;
        }

        //更新列表后，判断是否需要绑定运营商
        if (!TextUtils.isEmpty(bindChannelMobile_deencredit) ){
            if (OthersInfo_deencredit.getInstance_deencredit().getAddBindFlag_deencredit()){
                new BindAccountChannelDialog_Fr_deencredit(activity_deencredit, bindChannelMobile_deencredit,
                        null,
                        () -> HttpUtil_deencredit.getInstance_deencredit().addAccount_Fr_deencredit(
                                activity_deencredit,
                                bindChannelMobile_deencredit,
                                bindChannelMobile_deencredit,
                                OthersInfo_deencredit.getInstance_deencredit().getChannel_deencredit(),
                                () -> HttpUtil_deencredit.getInstance_deencredit().requestAccountList_Fr_deencredit(activity_deencredit,this::refreshAccountList_deencredit),
                                false
                        )

                ).show();
            }
        }

    }

    //添加卡
    private void addAccount_deencredit(BankAccount_Fr_deencredit account, int num){

        if (account == null) return;

        String desc = account.getWalletDesc_deencredit();

        View accountView = View.inflate(mView_deencredit.getContext(), R.layout.item_account_fr_deencredit, null);
        TextView titleTv = accountView.findViewById(R.id.iaf_tv_title);
        ImageView logoIv = accountView.findViewById(R.id.iaf_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iaf_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iaf_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iaf_ib_delete);

        titleTv.append(" "+num);
        mobileTv.setText(account.getWalletMobile_deencredit());
        channelTv.setText(account.getWalletDesc_deencredit());

        //删除卡
        deleteIb.setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"DELETE_CLICK",account.getWalletMobile_deencredit());
            new DeleteAccountDialog_Fr_deencredit(activity_deencredit,account, page_deencredit,
                    () -> HttpUtil_deencredit.getInstance_deencredit().requestAccountList_Fr_deencredit(activity_deencredit,this::refreshAccountList_deencredit)).show();
        });

        groupLayout_deencredit.addView(accountView);

        //获取图标
        if (AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_deencredit())) return;
                    if (!AppShowCustomize_deencredit.getInstance_deencredit().hasAccountChannel_deencredit()) return;
                    URL url = new URL(account.getWalletLogo_deencredit());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppShowCustomize_deencredit.getInstance_deencredit().setChannelLogoByDesc_deencredit(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(desc));

    }

}
